package com.wx.mall.sys.controller;


import com.wx.mall.common.ResultVo;
import com.wx.mall.common.utils.WebTools;
import com.wx.mall.sys.entity.Platform;
import com.wx.mall.sys.service.IPlatformService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author LiXuQiang
 * @since 2020-03-16
 */
@RestController
@RequestMapping("/platform")
@Api(tags = "平台信息接口")
@Slf4j
public class PlatformController {

    @Value("${localhost.file.path}")
    private String filePath;
    @Autowired
    private IPlatformService iPlatformService;

    @ApiOperation("查询平台信息")
    @GetMapping("/get")
    public String get(){
        return new ResultVo(ResultVo.ErrorCode.SUCCESS,"", iPlatformService.get()).toString();
    }

    @ApiOperation("保存平台信息")
    @PostMapping("/save")
    public String save(@RequestParam(value = "file",required = false) @ApiParam("上传的平台LOGO") MultipartFile file,
                       @ApiParam("平台名称") String name,
                       @ApiParam("平台标题") String title,
                       @ApiParam("平台描述") String description,
                       @ApiParam("关键词") String keywords,
                       @ApiParam("客服热线") String hotline,
                       @ApiParam("客服邮箱") String email,
                       @ApiParam("平台内容") String content) throws IOException {
        Platform platform = new Platform();
        platform.setName(name);
        platform.setTitle(title);
        platform.setDescription(description);
        platform.setKeywords(keywords);
        platform.setHotline(hotline);
        platform.setEmail(email);
        platform.setContent(content);
        String imgId = WebTools.getRandomID()+".png";
        if(file != null){
            WebTools.cacheFile(file,filePath,imgId);
            platform.setLogo("/showImg?id="+imgId+"&token=0");
        }else{
            platform.setLogo(iPlatformService.get().getLogo());
        }
        iPlatformService.save2(platform);
        return new ResultVo(ResultVo.ErrorCode.SUCCESS,"提交成功！","").toString();
    }


}

